﻿Imports Lourdes.BackEnd.BC
Imports Lourdes.BackEnd.Modelo
Imports Lourdes.BackEnd.View
Public Class frmMercaderiaBuscar
  Public Event ProductoSeleccionado(ByVal pProducto As vProducto)
  Private _ListPrecioId As Integer
  Public Property ListaPrecioId() As Integer
    Get
      Return _ListPrecioId
    End Get
    Set(ByVal value As Integer)
      _ListPrecioId = value
    End Set
  End Property

  Public Sub GetProductoByFilters(ByVal pListPreciosId As Integer, ByVal pMercNum As String, ByVal pMercDesc As String, Optional pParent As Form = Nothing)
    Dim bcMerc As BC_Mercaderia
    Dim lstvProd As List(Of vProducto)
    Try
      bcMerc = New BC_Mercaderia
      lstvProd = bcMerc.GetMercViewByFilters(pListPreciosId, pMercDesc, pMercNum)

      If lstvProd.Count = 1 Then
        RaiseEvent ProductoSeleccionado(lstvProd.FirstOrDefault)
      Else
        txtCodigo.Text = pMercNum
        txtDescripcion.Text = pMercDesc
        btnFiltrar_Click(Me, Nothing)
        If pParent IsNot Nothing Then
          ShowDialog(pParent)
        Else
          ShowDialog()
        End If

        If lstvProd.Count > 1 Then
          grdDatos.Focus()
          grdDatos.Rows.FirstOrDefault.Selected = True
        Else
          txtCodigo.Focus()
        End If
      End If

      CargarGrid(lstvProd)
    Catch ex As Exception
      Utils.ManejoErrores(ex)
    End Try
  End Sub

  Private Sub btnFiltrar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFiltrar.Click
    CargarGrid()
  End Sub

  Private Sub CargarGrid()
    Dim bcMerc As BC_Mercaderia
    Dim lstvProd As List(Of vProducto)
    Try
      bcMerc = New BC_Mercaderia
      lstvProd = bcMerc.GetMercViewByFilters(_ListPrecioId, txtDescripcion.Text, txtCodigo.Text)
      CargarGrid(lstvProd)
    Catch ex As Exception
      Utils.ManejoErrores(ex)
    End Try
  End Sub

  Private Sub CargarGrid(ByVal pLstvProducto As List(Of vProducto))
    Try
      With grdDatos
        .DataSource = pLstvProducto
        .DataBind()
        .Focus()
        If pLstvProducto.Count > 0 Then
          .Rows.FirstOrDefault.Selected = True
        End If
      End With
    Catch ex As Exception
      Utils.ManejoErrores(ex)
    End Try
  End Sub

  Private Sub grdDatos_DoubleClickCell(ByVal sender As Object, ByVal e As Infragistics.Win.UltraWinGrid.DoubleClickCellEventArgs) Handles grdDatos.DoubleClickCell
    SeleccionarMerca()
  End Sub

  Private Sub grdDatos_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles grdDatos.KeyPress
    If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then
      SeleccionarMerca()
    End If
  End Sub

  Private Sub SeleccionarMerca()
    If grdDatos.Selected.Rows.Count > 0 Then
      'MessageBox.Show(grdDatos.Selected.Rows(0).Cells("PersonaId").Value)
      Dim id As Integer = grdDatos.Selected.Rows(0).Cells("MercaderiaId").Value
      Dim lstProdu As List(Of vProducto) = grdDatos.DataSource
      Dim unProdu As vProducto = (From p In lstProdu Where p.MercaderiaId = id).FirstOrDefault
      'MessageBox.Show(unaPersona.Nombre & " " & unaPersona.Apellido)
      RaiseEvent ProductoSeleccionado(unProdu)
      Me.Close()
      'Me.Dispose()
    End If
  End Sub
End Class